home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 144 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.1 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Count lines in file?
  5. Date: Tue, 02 Jan 96 16:55:50 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <820601750snz@genesis.demon.co.uk>
  9. References: <4bfnqu$btj@news1.netzone.com> <4bi4nr$3qe@castle.nando.net> <4bk30e$l08@news1.netzone.com> <Pine.A32.3.91.951224222853.23094E@red.weeg.uiowa.edu> <4bm9tk$h5a@crl14.crl.com> <820334651snz@genesis.demon.co.uk> <4c4v8b$kb6@crl5.crl.com>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <4c4v8b$kb6@crl5.crl.com> kossick@crl.com "Paul J. Kossick" writes:
  16.  
  17. >That's an interesting point I thought of when talking to someone else 
  18. >about this via E-Mail...I would think that fgets would be SLOWER than 
  19. >getc, since it actually has to execute code similar to getc to fill the 
  20. >buffer in the first place...as well as other work such as checking to see 
  21. >if the buffer is full.
  22.  
  23. Both getc and fgets have to perform these checks on every call (or macro
  24. invocation). Since fgets should be called fewer times than getchar the
  25. fgets version may well have lower overhead if the library is implemented
  26. reasonably.
  27.  
  28. > Since you don't NEED to retain the lines intact 
  29. >in order to count them, a buffer is basicly surplusage.
  30.  
  31. There are overheads whichever way you do it, it just depends which are
  32. greater. The fread solution gives the implementation the best chance of
  33. avoiding this extra buffering.
  34.  
  35. >Oh, before someone else points it out:  One problem with getc might be if 
  36. >the last line of the program doesn't have a terminating newline 
  37. >character, i.e. it terminates with the eof.  This could cause the count 
  38. >to be off by one in such cases.
  39.  
  40. It depends on your definition of a 'line'. It is reasonable to say is that
  41. of the final sequence of characters is not terminated by '\n' it is not
  42. a valid line.
  43.  
  44. -- 
  45. -----------------------------------------
  46. Lawrence Kirby | fred@genesis.demon.co.uk
  47. Wilts, England | 70734.126@compuserve.com
  48. -----------------------------------------
  49.